using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DDF.Data;
using DDF.Data.Providers;
using System.Data;
using System.Linq.Expressions;
using System.Configuration;
using ARR.Entities.Gateways;

namespace ARR.DataModel.Repositories
{
	public static partial class RangeReviewsRepository
	{
		private static string connString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString;
		
		public class RangeReviewsDbo : DatabaseObject
        {
            public RangeReviewsDbo()
            {
                Name = "RangeReviews";
                GeneratedClassName = "RangeReviews";
                SequenceName = "N/A";
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Id", ColumnType = "int", IsIdentity = true, IsNullable = false, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "Id" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "CountryId", ColumnType = "smallint", IsIdentity = false, IsNullable = false, Length = 2, Precision = 5, Scale = 0, GeneratedPropertyName = "CountryId" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "TargetCurrencyId", ColumnType = "int", IsIdentity = false, IsNullable = false, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "TargetCurrencyId" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Title", ColumnType = "varchar", IsIdentity = false, IsNullable = false, Length = 200, Precision = 0, Scale = 0, GeneratedPropertyName = "Title" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "TotalMonths", ColumnType = "int", IsIdentity = false, IsNullable = false, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "TotalMonths" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "FirstMonth", ColumnType = "date", IsIdentity = false, IsNullable = false, Length = 3, Precision = 10, Scale = 0, GeneratedPropertyName = "FirstMonth" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "HasPriorYear", ColumnType = "bit", IsIdentity = false, IsNullable = false, Length = 1, Precision = 1, Scale = 0, GeneratedPropertyName = "HasPriorYear" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Bottom", ColumnType = "smallmoney", IsIdentity = false, IsNullable = false, Length = 4, Precision = 10, Scale = 4, GeneratedPropertyName = "Bottom" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "ReportDate", ColumnType = "datetime", IsIdentity = false, IsNullable = false, Length = 8, Precision = 23, Scale = 3, GeneratedPropertyName = "ReportDate" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "PYHistoryId", ColumnType = "int", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "PYHistoryId" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "OverSpaced", ColumnType = "money", IsIdentity = false, IsNullable = false, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "OverSpaced" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "UnderSpaced", ColumnType = "money", IsIdentity = false, IsNullable = false, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "UnderSpaced" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "StoreSegmentation1", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 50, Precision = 0, Scale = 0, GeneratedPropertyName = "StoreSegmentation1" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "StoreSegmentation2", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 50, Precision = 0, Scale = 0, GeneratedPropertyName = "StoreSegmentation2" });
              
            }
        }
		
		public static List<RangeReviewRow> Select(Expression<Func<RangeReviewRow, bool>> expression)
        {
            List<RangeReviewRow> rows = new List<RangeReviewRow>();

            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                IDataReader reader = database.Select<RangeReviewRow>(new RangeReviewsDbo(), expression);

                Load(reader, rows);
            }

            return rows;
        }
		
		public static List<RangeReviewRow> Select()
		{
			List<RangeReviewRow> rows = new List<RangeReviewRow>();

			using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
			{
				IDataReader reader = database.Select<RangeReviewRow>(new RangeReviewsDbo(), null);

				Load(reader, rows);
			}

			return rows;
		}
		
		public static List<RangeReviewRow> PagedSelect(Expression<Func<RangeReviewRow, bool>> filterExpression, Expression<Func<RangeReviewRow, object>> orderByExpression, int pageNumber, int pageSize)
		{
			List<RangeReviewRow> rows = new List<RangeReviewRow>();
			
			using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
			{
				IDataReader reader = database.PagedSelect<RangeReviewRow>(new RangeReviewsDbo(), filterExpression, orderByExpression, pageNumber, pageSize);

				Load(reader, rows);
			}

			return rows;
		}
		
		private static void Load(IDataReader reader, List<RangeReviewRow> RangeReviewList)
        {
            RangeReviewRow row;

            while (reader.Read())
            {
                row = new RangeReviewRow()
                {
					Id = Util.CastDbValue<int>(reader["Id"]),
					CountryId = Util.CastDbValue<int>(reader["CountryId"]),
					TargetCurrencyId = Util.CastDbValue<int>(reader["TargetCurrencyId"]),
					Title = Util.CastDbValue<string>(reader["Title"]),
					TotalMonths = Util.CastDbValue<int>(reader["TotalMonths"]),
					FirstMonth = Util.CastDbValue<DateTime>(reader["FirstMonth"]),
					HasPriorYear = Util.CastDbValue<bool>(reader["HasPriorYear"]),
					Bottom = Util.CastDbValue<decimal>(reader["Bottom"]),
					ReportDate = Util.CastDbValue<DateTime>(reader["ReportDate"]),
					PYHistoryId = Util.CastDbValue<int?>(reader["PYHistoryId"]),
					OverSpaced = Util.CastDbValue<decimal>(reader["OverSpaced"]),
					UnderSpaced = Util.CastDbValue<decimal>(reader["UnderSpaced"]),
					StoreSegmentation1 = Util.CastDbValue<string>(reader["StoreSegmentation1"]),
					StoreSegmentation2 = Util.CastDbValue<string>(reader["StoreSegmentation2"])
                };
                                
                RangeReviewList.Add(row);
            }

            reader.Close();
        }

			
		public static void Insert(RangeReviewRow row)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Insert<RangeReviewRow>(new RangeReviewsDbo(), row);
            }
        }

		public static void Update(RangeReviewRow row)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Update<RangeReviewRow>(new RangeReviewsDbo(), row);
            }
        }

		public static void Delete(Expression<Func<RangeReviewRow, bool>> expression)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Delete<RangeReviewRow>(new RangeReviewsDbo(), expression);
            }
        }

	}
}
	
